<script setup lang="ts">
import { getQuestionListBySubjectId, deleteQuestion } from '@/api/subject';
import { Question } from '@/api/subject/type';
import { router } from '@/router';
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { showDialog, showToast } from 'vant';

const subjectId = ref('');
const questionList = ref([] as Question[]);
const loading = ref(false);

onMounted(() => {
    const route = useRoute();
    const id = route.query.id;
    if (!Array.isArray(id)) {
        subjectId.value = id as string;
        fetchQuestionList();
    } else {
        showToast('题目集ID无效');
        router.push('/subject');
    }
})

const fetchQuestionList = () => {
    loading.value = true;
    getQuestionListBySubjectId(subjectId.value).then((res) => {
        questionList.value = res.data;
    }).catch(() => {
        showToast('获取题目列表失败');
    }).finally(() => {
        loading.value = false;
    });
}

const toCreate = () => {
    router.push({
        name: "QuestionCreate",
        query: {
            "id": subjectId.value
        }
    });
}

const goBack = () => {
    router.push({
        name: "SubjectHome",
        query: {
            "id": subjectId.value
        }
    });
}

const getQuestionTypeLabel = (type: string) => {
    const types: Record<string, string> = {
        'Choice': '选择题',
        'Judge': '判断题',
        'Blank': '填空题',
        'Essay': '简答题'
    };
    return types[type] || type;
}

const toData = () => {
    router.push({
        name: "ExerciseData",
        query: {
            "id": subjectId.value
        }
    })
}

const handleDelete = (questionId: string) => {
    showDialog({
        title: '确认删除',
        message: '确定要删除这道题目吗？删除后将无法恢复。',
        showCancelButton: true,
        confirmButtonText: '删除',
        confirmButtonColor: '#ee0a24',
    }).then(() => {
        deleteQuestion(questionId).then(() => {
            showToast('删除成功');
            fetchQuestionList();
        }).catch(() => {
            showToast('删除失败');
        });
    }).catch(() => {
        // 取消删除
    });
}
</script>

<template>
    <div class="question-manage">
        <div class="page-header">
            <div class="back-action" @click="goBack">
                <van-icon name="arrow-left" />
                <span>返回题目集</span>
            </div>
            <h1 class="page-title">题目管理</h1>
        </div>
        
        <div class="page-content">
            <div class="action-bar">
                <button class="action-button primary" @click="toCreate">
                    <van-icon name="plus" />
                    <span>创建题目</span>
                </button>
                <button class="action-button" @click="toData">
                    <van-icon name="data" />
                    <span>做题数据</span>
                </button>
                <button class="action-button" disabled>
                    <van-icon name="down" />
                    <span>导入题目</span>
                </button>
            </div>
            
            <div class="question-list">
                <van-skeleton title :row="3" :loading="loading" v-for="i in 3" :key="`skeleton-${i}`" v-if="loading"></van-skeleton>
                
                <van-empty 
                    v-if="!loading && questionList.length === 0" 
                    image="search"
                    description="暂无题目，请先添加题目"
                >
                    <template #bottom>
                        <van-button round type="primary" @click="toCreate">创建题目</van-button>
                    </template>
                </van-empty>
                
                <div v-if="!loading && questionList.length > 0" class="question-cards">
                    <div v-for="(question, index) in questionList" :key="index" class="question-card">
                        <div class="question-type-tag" :class="question.type.toLowerCase()">
                            {{ getQuestionTypeLabel(question.type) }}
                        </div>
                        <div class="question-content">
                            <div class="question-title">{{ question.title }}</div>
                            <div class="question-info">
                                <span class="question-id">ID: {{ question.id }}</span>
                                <span class="question-date">创建时间: {{ question.created }}</span>
                            </div>
                        </div>
                        <div class="question-actions">
                            <button class="delete-button" @click="handleDelete(question.id)">
                                <van-icon name="delete-o" />
                                <span>删除</span>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.question-manage {
    max-width: 1000px;
    margin: 0 auto;
    padding: 0 16px;
    
    .page-header {
        margin-bottom: 24px;
        
        .back-action {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            color: #606266;
            cursor: pointer;
            padding: 8px 0;
            font-size: 14px;
            margin-bottom: 8px;
            
            &:hover {
                color: #409eff;
            }
        }
        
        .page-title {
            font-size: 24px;
            font-weight: 600;
            color: #303133;
            margin: 0;
        }
    }
    
    .page-content {
        background-color: white;
        border-radius: 12px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
        overflow: hidden;
        
        .action-bar {
            display: flex;
            gap: 12px;
            padding: 20px 24px;
            border-bottom: 1px solid #ebeef5;
            
            .action-button {
                display: flex;
                align-items: center;
                gap: 6px;
                height: 40px;
                padding: 0 20px;
                border-radius: 20px;
                font-size: 14px;
                font-weight: 500;
                border: 1px solid #dcdfe6;
                background-color: #fff;
                color: #606266;
                cursor: pointer;
                transition: all 0.2s;
                
                &:hover:not(:disabled) {
                    background-color: #f5f7fa;
                }
                
                &:disabled {
                    opacity: 0.6;
                    cursor: not-allowed;
                }
                
                &.primary {
                    background-color: #409eff;
                    border-color: #409eff;
                    color: white;
                    
                    &:hover {
                        background-color: #66b1ff;
                        border-color: #66b1ff;
                    }
                }
            }
        }
        
        .question-list {
            padding: 20px 24px;
            
            .question-cards {
                display: flex;
                flex-direction: column;
                gap: 16px;
                
                .question-card {
                    display: flex;
                    position: relative;
                    border-radius: 8px;
                    border: 1px solid #ebeef5;
                    background-color: #fafafa;
                    overflow: hidden;
                    
                    &:hover {
                        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
                    }
                    
                    .question-type-tag {
                        width: 80px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        color: white;
                        font-size: 14px;
                        
                        &.choice {
                            background-color: #409eff;
                        }
                        
                        &.judge {
                            background-color: #67c23a;
                        }
                        
                        &.blank {
                            background-color: #e6a23c;
                        }
                        
                        &.essay {
                            background-color: #909399;
                        }
                    }
                    
                    .question-content {
                        flex: 1;
                        padding: 16px;
                        
                        .question-title {
                            font-size: 15px;
                            font-weight: 500;
                            color: #303133;
                            margin-bottom: 8px;
                            line-height: 1.5;
                        }
                        
                        .question-info {
                            display: flex;
                            flex-wrap: wrap;
                            gap: 16px;
                            font-size: 13px;
                            color: #909399;
                        }
                    }
                    
                    .question-actions {
                        display: flex;
                        align-items: center;
                        padding: 0 16px;
                        
                        .delete-button {
                            display: flex;
                            align-items: center;
                            gap: 4px;
                            height: 32px;
                            padding: 0 12px;
                            border-radius: 16px;
                            background-color: #fff2f0;
                            border: 1px solid #ffccc7;
                            color: #f56c6c;
                            font-size: 13px;
                            cursor: pointer;
                            transition: all 0.2s;
                            
                            &:hover {
                                background-color: #fde2e2;
                                border-color: #fbc4c4;
                            }
                        }
                    }
                }
            }
        }
    }
}

// 响应式调整
@media (max-width: 768px) {
    .action-bar {
        flex-wrap: wrap;
        
        .action-button {
            flex: 1;
        }
    }
    
    .question-card {
        flex-direction: column;
        
        .question-type-tag {
            width: 100% !important;
            padding: 8px 0;
        }
        
        .question-actions {
            padding: 8px 16px !important;
            justify-content: flex-end;
            border-top: 1px solid #ebeef5;
        }
    }
}
</style>